import { getOrderStatus } from "@/api/modules/transactionManagement";
import { Space } from "antd";
import { keys, map, join, sumBy, isEmpty } from "lodash";
import ProductThumbnailName from "./components/ProductThumbnailName";
import JudgeStatus from "./JudgeStatus";
import styles from "./index.module.less";

export const getSearchOptions = (info: string) => {
	return [
		{
			type: "input",
			placeholder: "订单编号",
			name: "order_id"
		},

		{
			type: "select",
			placeholder: "订单状态",
			name: "order_status",
			params: info ?? {},
			request: async params => {
				const res = await getOrderStatus(params);
				const data = res?.data ?? {};
				return map(keys(data), item => ({
					label: data[item],
					value: item
				}));
			}
			// fieldProps: {
			// 	fieldNames: {
			// 		label: "order_status_name",
			// 		value: "order_status_id"
			// 	}
			// }
		},
		{
			type: "input",
			placeholder: "商品名称",
			name: "product_name"
		},
		{
			type: "input",
			placeholder: "店铺名称",
			name: "shop_name"
		}
	];
};

export enum TAB_VALUES {
	//全部
	all = "all",
	//待发货
	sale_wait = "noSend",
	//已发货
	sales = "haveSend",
	//退款中
	inRefunded = "aftersales",
	//交易成功的订单
	success = "success",
	//关闭的订单
	close = "close",
	//继续发货
	continueSend = "continueSend"
}

export const TABS = [
	{
		label: "全部",
		key: TAB_VALUES.all
	},
	{
		label: "待发货",
		key: TAB_VALUES.sale_wait
	},
	{
		label: "已发货",
		key: TAB_VALUES.sales
	},
	{
		label: "继续发货",
		key: TAB_VALUES.continueSend
	},
	{
		label: "待审核售后",
		key: TAB_VALUES.inRefunded
	},
	{
		label: "交易成功的订单",
		key: TAB_VALUES.success
	},
	{
		label: "关闭的订单",
		key: TAB_VALUES.close
	}
];

export const TABLE_COLUMNS = [
	// {
	// 	title: "订单编号",
	// 	dataIndex: "order_id",
	// 	width: 200
	// 	// fixed: "left"
	// },
	{
		title: "店铺",
		dataIndex: "a",
		width: 200,
		// fixed: "left",
		align: "left",
		className: styles.shop,
		render: (_, record) => <div style={{ textAlign: "left" }}>{record?.shop_info?.shop_name ?? "-"}</div>
	},
	{
		title: "商品",
		dataIndex: "itemsList",
		width: 260,
		ellipsis: true,
		render: (_, record) => {
			return <ProductThumbnailName data={record?.itemsList ?? []} />;
		}
	},
	{
		title: "单价",
		dataIndex: "itemsList",
		width: 140,
		renderText: v => {
			// if (v?.length === 1) return `￥${v?.[0]?.format_spec_price}`;
			return v?.[0]?.format_spec_price;

			return "-";
		}
	},
	{
		title: "数量",
		dataIndex: "itemsList",
		width: 140,
		renderText: v => {
			return sumBy(v, item => item?.spec_num ?? 0);
		}
	},
	{
		title: "交易状态",
		dataIndex: "status_info",
		width: 160,
		renderText: v => v?.order_status_name ?? "-"
	},
	{
		title: "实付款",
		dataIndex: "format_total_pay_amount",
		width: 200,
		render: (_, record) => {
			return (
				<>
					<span>{`￥${record?.format_total_pay_amount ?? 0}`}</span>
					{Boolean(record?.format_total_shipping_fee) && <div>{`(含快递费:￥${record?.format_total_shipping_fee ?? 0})`}</div>}
				</>
			);
		}
	},
	{
		title: "售后",
		dataIndex: "status_info",
		width: 200,
		render: (_, record) => {
			const {
				isWaitSales,
				isSales,
				isInRefunded,
				isSalesSuccess,
				isInRefundedWaitAudit,
				isContinueSend,
				isPartialDelivery,
				isInRefundedComplete,
				isMerchantInRefundedRefuse
			} = JudgeStatus;

			// if (isWaitSales(record)) {
			// 	return "未发货";
			// }

			// if (isSales(record)) {
			// 	return "已发货";
			// }

			if (isInRefundedComplete(record)) {
				return (
					<>
						<div style={{ color: "#E82F2F" }}>退款成功</div>
						{isEmpty(record?.order_have_send_spec_ids) && <div>未发货</div>}
					</>
				);
			}

			if (isMerchantInRefundedRefuse(record)) {
				return <div style={{ color: "#E82F2F" }}>卖家已拒绝退款申请</div>;
			}

			if (isInRefundedWaitAudit(record)) {
				return (
					<>
						<div style={{ color: "#E82F2F" }}>买家申请退款</div>
						<div>待卖家审核</div>
					</>
				);
			}

			return record?.aftersale_status_info?.order_aftersales_status_name ?? "-";
		}
	}

	// {
	// 	title: "创建时间",
	// 	dataIndex: "created_at",
	// 	width: 160
	// }
];
